/**
 * 
 */
package edu.cmu.cs.webapp.ioms.ui.dialogs.order;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.ui.providers.order.ItemsTableViewerContentProvider;
import edu.cmu.cs.webapp.ioms.ui.providers.order.ItemsTableViewerLabelProvider;

/**
 * @author Christian
 * 
 */
public class ViewOrderItemsDialog extends Dialog {

	private PurchaseOrder order;
	private TableViewer itemsViewer;
	private Table table;

	public ViewOrderItemsDialog(Shell parentShell, PurchaseOrder order) {
		super(parentShell);
		this.order = order;
	}

	@Override
	protected Control createDialogArea(Composite parent) {

		Composite composite = (Composite) super.createDialogArea(parent);

		GridLayout gl = new GridLayout(1, false);
		composite.setLayout(gl);

		createTableViewer(composite);
		return composite;
	}

	protected void configureShell(final Shell shell) {
		super.configureShell(shell);
		shell.setText("Items of order#" + this.order.getOrder_id());
	}

	private void createTableViewer(Composite container) {

		/*
		 * Create table
		 */
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;

		table = new Table(container, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL
				| SWT.V_SCROLL | SWT.FULL_SELECTION);

		table.setLayoutData(gridData);

		table.setHeaderVisible(true);
		table.setLinesVisible(false);

		// Product
		TableColumn productColumn = new TableColumn(table, SWT.NONE);
		productColumn.setText("Product"); //$NON-NLS-1$
		productColumn.setWidth(110);

		// order quantity
		TableColumn quantityColumn = new TableColumn(table, SWT.NONE);
		quantityColumn.setText("Quantity"); //$NON-NLS-1$
		quantityColumn.setWidth(90);

		// Package Spec
		TableColumn pacColumn = new TableColumn(table, SWT.NONE);
		pacColumn.setText("Unit"); //$NON-NLS-1$
		pacColumn.setWidth(90);

		// Price per unit
		TableColumn pricColumn = new TableColumn(table, SWT.NONE);
		pricColumn.setText("Unit Price"); //$NON-NLS-1$
		pricColumn.setWidth(90);

		// Gross sales
		TableColumn salesColumn = new TableColumn(table, SWT.NONE);
		salesColumn.setText("Gross Sales"); //$NON-NLS-1$
		salesColumn.setWidth(100);

		// Remarks
		TableColumn remarkColumn = new TableColumn(table, SWT.NONE);
		remarkColumn.setText("Remarks"); //$NON-NLS-1$
		remarkColumn.setWidth(150);

		/*
		 * Create viewer
		 */
		itemsViewer = new TableViewer(table);

		itemsViewer.setUseHashlookup(true);
		itemsViewer.setLabelProvider(new ItemsTableViewerLabelProvider());
		itemsViewer.setContentProvider(new ItemsTableViewerContentProvider());

		itemsViewer.setInput(order);

	}

}
